﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.backend
{
    public partial class Tinh : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCombox();
                LoadData();
            }
        }
        public void LoadCombox()
        {
            droQuocGia.DataSource = BLL.Admin.QuocGiaHelper.List_QuocGia();
            droQuocGia.DataTextField = "Ten";
            droQuocGia.DataValueField = "QuocGiaId";
            droQuocGia.DataBind();
            //
            droQuocGiaSearch.DataSource = BLL.Admin.QuocGiaHelper.List_QuocGia();
            droQuocGiaSearch.DataTextField = "Ten";
            droQuocGiaSearch.DataValueField = "QuocGiaId";
            droQuocGiaSearch.DataBind();
        }
        public void Reset()
        {
            txtTinh.Text = "";
            btSave.Visible = true;
            btUpdate.Visible = false;
        }
        public void LoadData()
        {
            rptTinh.DataSource = BLL.Admin.TinhHelper.List_Tinh_Search(int.Parse(droQuocGiaSearch.SelectedValue),txtSearch.Text);
            rptTinh.DataBind();
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTinh.Text))
            { 
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa nhập tên tỉnh/thành phố!');", true);
                 return;
            }
            if (droQuocGia.Items.Count < 1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa tạo quốc gia!');", true);
                return;
            }
            BLL.Admin.TinhHelper.Add_Tinh(int.Parse(droQuocGia.SelectedValue),txtTinh.Text);
            LoadData();
            Reset();
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTinh.Text))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa nhập tên tỉnh/thành phố!');", true);
                return;
            }
            BLL.Admin.TinhHelper.Update_Tinh(int.Parse(lbTinhId.Text),int.Parse(droQuocGia.SelectedValue),txtTinh.Text);
            LoadData();
            Reset();
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void rptTinh_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int tinhid = int.Parse(e.CommandArgument.ToString());
                lbTinhId.Text = tinhid.ToString();
                DAL.tbl_DM_Tinh tinh = BLL.Admin.TinhHelper.Get_TinhId(tinhid);
                txtTinh.Text = tinh.Ten;
                droQuocGia.SelectedValue = tinh.QuocGiaId.ToString();
                btSave.Visible = false;
                btUpdate.Visible = true;
            }
        }

        protected void droQuocGiaSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}